package net.cohoivang.news;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import net.cohoivang.web.OurConnection;

public class NewsManager {
	private static NewsManager instance = new NewsManager();
	
	private final HashMap<Integer, News> newsMap = new HashMap<Integer, News>();
	private final List<News> allNews = new LinkedList<News>();	
	
	public static NewsManager getInstance() {
		return instance;
	}	
	
	private NewsManager() {}
	
	public static void initialize(OurConnection connection) throws SQLException {
		NewsDatabase.initialize(connection);
		NewsDatabase.getInstance().loadAllNews();
	}
	
	public void initializeToTopNews() {
		System.out.println(" put to top list");
		for (News n : allNews) {
			if (n.isCurrentTop()) {
				for (EnableToTopListener e : n.listener) {
					e.enableToTop(n, -1);
				}
			}
		}
	}
	
	void putNews(News news) {
		newsMap.put(news.getNewsId(), news);
		allNews.add(news);
	}
	
	
}
